package com.xgry.bpm.api.business.griddata.service.impl;

import com.xgry.bpm.api.business.griddata.entity.GridData;
import com.xgry.bpm.api.business.griddata.mapper.GridDataDao;
import com.xgry.bpm.api.business.griddata.form.GridDataForm;
import com.xgry.bpm.api.business.griddata.service.GridDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xgry.bpm.api.business.griddata.vo.GridDataVO;
import org.springframework.util.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * GridData服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-13
 */
@Service
        public class GridDataServiceImpl extends ServiceImpl<GridDataDao, GridData>implements GridDataService {

        @Override
        public int save(GridDataForm record){
    GridData data=this.convertDO(record);
        return baseMapper.insert(data);
        }

        @Override
        public int updateById(GridDataForm record){
        //修改时间
    GridData data=this.convertDO(record);
        return baseMapper.updateById(data);
        }

        @Override
        public int deleteById(String id){
        return baseMapper.deleteById(id);
        }

        @Override
        public GridDataVO selectById(String id){
        GridData data=baseMapper.selectById(id);
        GridDataVO vo=new GridDataVO();
        BeanUtils.copyProperties(data,vo);
        return vo;
        }

        @Override
        public List<GridDataVO>selectAll(GridDataForm record){
    GridData data=this.convertDO(record);
        QueryWrapper<GridData>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(data);
        List<GridData>list=baseMapper.selectList(queryWrapper);
        return this.convert(list);
        }

        @Override
        public IPage<GridDataVO>selectPage(GridDataForm record){
        // form -> do 转换
    GridData data=this.convertDO(record);
        // 分页数据设置
        Page<GridData>page=new Page<>(record.getCurrent(),record.getSize());
        // 查询条件
        QueryWrapper<GridData>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(data);
        IPage<GridData>list=baseMapper.selectPage(page,queryWrapper);
        IPage<GridDataVO>iPage=new Page<>();
        iPage.setRecords(this.convert(list.getRecords()));
        iPage.setCurrent(list.getCurrent());
        iPage.setSize(list.getSize());
        iPage.setTotal(list.getTotal());
        iPage.setPages(list.getPages());
        return iPage;
        }

        @Override
        public IPage<GridData>getPage(IPage<GridData>page,QueryWrapper<GridData>queryWrapper){
        return baseMapper.selectPage(page,queryWrapper);
        }


        /**
        * Form -> Do
        * @param form 对象
        * @return Do对象
        */
        private GridData convertDO(GridDataForm form){
            GridData data=new GridData();
            BeanUtils.copyProperties(form,data);
        return data;
        }

        /**
        * Do -> VO
        * @param list 对象
        * @return VO对象
        */
        private List<GridDataVO>convert(List<GridData>list){
        List<GridDataVO> gridDataList=new ArrayList<>();
        if(CollectionUtils.isEmpty(list)){
        return gridDataList;
        }
        for(GridData source:list){
    GridDataVO target=new GridDataVO();
        BeanUtils.copyProperties(source,target);
    gridDataList.add(target);
        }
        return gridDataList;
        }

        }
